package com.example.project_dish.Order;

import static com.example.project_dish.method.return_main;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.project_dish.Capital.CapitalActivity;
import com.example.project_dish.Dishes.SelectDishActivity;
import com.example.project_dish.R;
import com.example.project_dish.method;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class MyOrderActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView icon_text;
    private Button select_dish;
    private Button capital_dish;
    private Button order_dish;
    private ImageView top_image;
    private ImageView dishes_image;
    private TextView dishes_name;
    private TextView dishes_price;
    private TextView dishes_comment;
    private ImageView dishesreduce;
    private TextView order_none;

    private List<OrderEvent> data;
    private AdapterOrder adapterOrder;
    private ListView orderList;
    private Button order_send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);

        // 设置图标大小
        icon_text = findViewById(R.id.icon_text);
        Drawable drawableLeft = getResources().getDrawable(R.drawable.star);
        drawableLeft.setBounds(0,0,35,35);
        icon_text.setCompoundDrawables(drawableLeft,null,null,null);

        // 设置状态栏透明
        method.status_bar(this);
        if(this.getSupportActionBar() != null){
            this.getSupportActionBar().hide();
        }

        // 设置背景图片透明度
        top_image = findViewById(R.id.top_image);
        method.setBackground(null,top_image);

        // 设置部分字体颜色
        String icon_text_str = "<font color='#f2cb51'>4.6</font> 月售3000 商家十二分钟内··";
        icon_text.setText(Html.fromHtml(icon_text_str));

        //获取按钮
        select_dish = findViewById(R.id.select_dish);
        select_dish.setOnClickListener(this);
        capital_dish = findViewById(R.id.capital_dish);
        capital_dish.setOnClickListener(this);
        order_dish = findViewById(R.id.order_dish);
        order_dish.setOnClickListener(this);
        order_send = findViewById(R.id.order_send);
        order_send.setOnClickListener(this);

        order_none = findViewById(R.id.order_none);
        dishes_name = findViewById(R.id.dishesName);
        dishes_price = findViewById(R.id.dishesPrice);
        dishes_comment = findViewById(R.id.dishesComment);
        dishes_image = findViewById(R.id.dishesImage);
        dishesreduce = findViewById(R.id.dishesReduce);

        initView();

        return_main(top_image);
    }

    private void initView() {
        //注册EventBus
        if (!EventBus.getDefault().isRegistered(this))
        {
            EventBus.getDefault().register(this);
        }

        // 初始化评论列表
        orderList = (ListView) findViewById(R.id.order_list);
        // 初始化数据
        data = new ArrayList<>();
        // 初始化适配器
        adapterOrder = new AdapterOrder(getApplicationContext(), data);
        // 为评论列表设置适配器
        orderList.setAdapter(adapterOrder);
    }

    @Subscribe(threadMode = ThreadMode.POSTING,sticky = true)//true 就是允许接收粘性事件
    public void onEventMainThread(OrderEvent event){
        if(event.getName() != null){
            order_none.setVisibility(View.GONE);
            OrderEvent orderEvent= new OrderEvent(event.getComment(),event.getId(),event.getName(),event.getPrice(), R.drawable.reduce);
            adapterOrder.addComment(orderEvent);
        }else {
            order_none.setText("点我去选择钟意滴菜");
            order_none.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.select_dish:
                Intent intent = new Intent(MyOrderActivity.this, SelectDishActivity.class);
                startActivity(intent);
                break;
            case R.id.capital_dish:
                Intent intent1 = new Intent(MyOrderActivity.this, CapitalActivity.class);
                startActivity(intent1);
                break;
            case R.id.order_dish:
                Intent intent2 = new Intent(MyOrderActivity.this, MyOrderActivity.class);
                startActivity(intent2);
                break;
            case R.id.order_none:
                Intent intent3 = new Intent(MyOrderActivity.this, SelectDishActivity.class);
                startActivity(intent3);
                break;
            case R.id.order_send:
                if (data.isEmpty()){
                    Toast.makeText(MyOrderActivity.this,"请选择菜品",Toast.LENGTH_SHORT).show();
                    order_none.setVisibility(View.VISIBLE);
                }else {
                    AlertDialog.Builder builder = new AlertDialog.Builder(MyOrderActivity.this);
                    builder.setTitle("确定要提交订单嘛");
                    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(MyOrderActivity.this,"请您评价一下味道",Toast.LENGTH_SHORT).show();
                            for (;;){
                                int u = 0;
                                data.remove(data.get(u));
                                if (data.isEmpty()){
                                    break;
                                }
                                adapterOrder.notifyDataSetChanged();
                                u++;
                            }
                            order_none.setVisibility(View.VISIBLE);
                            Intent intent1 = new Intent(MyOrderActivity.this, CapitalActivity.class);
                            startActivity(intent1);
                        }
                    });
                    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            dialogInterface.dismiss();
                        }
                    });
                    builder.show();
                }

        }
    }
}